<template>
  <el-form ref="formRef" :inline="true" :model="form" :rules="rules" label-width="80px">
    <el-divider content-position="left"><span class="divider-css">▶患者信息◀</span></el-divider>
    <el-row :gutter="5">
      <el-col :span="5">
        <el-form-item label="患者信息：" prop="patientId">
          <span class="mr5">
            {{ patientInfo.name }}
          </span>
          <span>{{ patientInfo.ageAll }}
            <svg-icon :icon-class="dictStore.getMateDictInfo(dictDataMap['sys_user_sex'], patientInfo.sex)
              .dictValue === '1'
              ? 'man'
              : 'woman'
              "/></span>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="住院号：" prop="hospitalNo">
          <span>{{ patientInfo.hospitalNo }}</span>
        </el-form-item>
      </el-col>
      <el-col :span="5">
        <el-form-item label="户口地址：" prop="registAddress">
          <span>{{ patientInfo.registAddress }}</span>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="现住址：" prop="address">
          <span>{{ patientInfo.address }}</span>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="电话：" prop="phone">
          <span>{{ patientInfo.phone }}</span>
        </el-form-item>
      </el-col>
    </el-row>

    <el-divider content-position="left"><span class="divider-css">▶开单信息◀</span></el-divider>
    <el-form-item label="开单科室" prop="applyDeptId">
      <el-input v-model="form.applyDeptName" :disabled="true" class="input-width" placeholder="请输入手术开单科室"/>
    </el-form-item>
    <el-form-item label="开单医生" prop="applyUser">
      <el-input v-model="form.applyUserName" :disabled="true" class="input-width" placeholder="请输入手术开单医生"/>
    </el-form-item>
    <el-form-item label="手术时间" prop="scheduledDate" style="width: 250px;">
      <el-date-picker v-model="form.scheduledDate" :clearable="false" :disabled="form.orderIds != ''" placeholder="请选择手术时间"
                      type="datetime"
                      value-format="YYYY-MM-DD HH:mm:ss"/>
    </el-form-item>
    <el-form-item label="手术室" prop="surgeryId">
      <dept-list :clearable="false" :dept-attribute-str="'12'" :dept-id="form.surgeryId" :disabled="form.orderIds != ''"
                 :org-id="form.orgId"
                 :placeholder="'请选择手术室'" class="input-width" @deptChange="deptChange"/>
    </el-form-item>
    <el-form-item label="手术间" prop="surgeryRoomNo">
      <el-select v-model="form.surgeryRoomId" :disabled="form.orderIds != ''" class="input-width"
                 placeholder="请选择手术间"
                 @change="surgeryRoomNoChange">
        <el-option v-for="item in surgeryRoomList" :label="item.roonNo" :value="item.surgeryRoomId"/>
      </el-select>
    </el-form-item>
    <el-form-item label="手术类型" prop="emergencyFlag">
      <el-select v-model="form.emergencyFlag" :disabled="form.orderIds != ''" class="input-width"
                 placeholder="请选择手术类型">
        <el-option v-for="dict in dictDataMap['surgery_type']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="麻醉科室" prop="anesthesiaDeptId">
      <dept-list :clearable="false" :dept-attribute-str="'13'" :dept-id="form.anesthesiaDeptId" :disabled="form.orderIds != ''"
                 :org-id="form.orgId" :placeholder="'请选择麻醉科室'" class="input-width"
                 @deptChange="anesthesiaDeptIdChange"/>
    </el-form-item>
    <el-form-item label="麻醉方法" prop="anesthesiaMethod">
      <el-select v-model="form.anesthesiaMethod" :disabled="form.orderIds != ''" class="input-width"
                 placeholder="请选择麻醉方法">
        <el-option v-for="dict in dictDataMap['anesthesia_method']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="计划台次" prop="plannedSort">
      <el-input v-model="form.plannedSort" :disabled="form.orderIds != ''" placeholder="请输入计划台次" style="width: 90px;"
                type="number"/>
    </el-form-item>

    <el-divider content-position="left"><span class="divider-css">▶手术信息◀</span></el-divider>
    <surgery-name-list ref="surgeryNameListRef" :org-id="form.orgId" :surgery-apply-info="form"/>


    <el-divider content-position="left"><span class="divider-css">▶患者状况◀</span></el-divider>
    <el-form-item label="术前主要诊断" label-width="100px" prop="diagBeforeOperation">
      <el-input v-model="form.diagBeforeOperation" :disabled="true" placeholder="请输入术前主要诊断"
                style="width: 400px;"/>
    </el-form-item>
    <el-form-item label="病情说明" prop="patientCondition">
      <el-select v-model="form.patientCondition" :disabled="form.orderIds != ''" placeholder="请选择病情说明"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['disease_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="供血方式" prop="provideWay">
      <el-select v-model="form.provideWay" :disabled="form.orderIds != ''" placeholder="请选择供血方式"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['provide_way_dict']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="隔离标志" prop="isolationIndicator">
      <el-select v-model="form.isolationIndicator" :disabled="form.orderIds != ''" placeholder="请选择隔离标志"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['isolation_grade']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="艾滋标记" prop="hivFlag">
      <el-select v-model="form.hivFlag" :disabled="form.orderIds != ''" placeholder="请选择艾滋标记"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="乙肝标记" prop="hbvFlag">
      <el-select v-model="form.hbvFlag" :disabled="form.orderIds != ''" placeholder="请选择乙肝标记"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否梅毒" prop="syphilisFlag">
      <el-select v-model="form.syphilisFlag" :disabled="form.orderIds != ''" placeholder="请选择是否梅毒"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否洗手" prop="washHands">
      <el-select v-model="form.washHands" :disabled="form.orderIds != ''" placeholder="请选择是否洗手"
                 style="width: 90px;">
        <el-option v-for="dict in dictDataMap['sys_yes_no']" :key="dict.dictValue" :label="dict.dictLabel"
                   :value="dict.dictValue"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import {listSurgeryRoom,} from "@/api/surgery/surgeryRoom";
import {useDictStoreHook} from "@/store/modules/dict.ts";
import {useUserStoreHook} from "@/store/modules/user.ts";
import SurgeryNameList from "@/views/surgery/apply/surgeryNameList.vue"

const dictStore = useDictStoreHook();
const dictDataMap = ref(dictStore.isTypeGetData('surgery_status', 'incision_grade', 'anesthesia_method', 'sys_user_sex',
  'surgery_type', 'operation_grade', 'isolation_grade', 'disease_dict', 'sys_yes_no', 'provide_way_dict'))
const {proxy} = getCurrentInstance() as any;

const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const formRef = ref();
const surgeryNameListRef = ref()
// 手术室下对应的手术间
const surgeryRoomList = ref<any[]>([])

const props = defineProps({
  patientInfo: {
    type: Object,
    default: () => {
    },
  },
  surgeryApplyId: {
    type: String,
    default: "",
  },
})


const form = ref<any>({
  surgeryApplyId: "",
  clinicId: "",
  visitId: "",
  patientId: "",
  inoroutFlag: "",
  surgeryNumber: 0,
  scheduledDate: proxy.$parseTime(new Date()),
  surgeryApplyDate: "",
  surgeryId: "",
  surgeryName: "",
  surgeryRoomId: "",
  surgeryNoName: "",
  diagBeforeOperation: "",
  diagBeforeOperationCode: "",
  patientCondition: "3",
  provideWay: "1",
  surgeryScale: "",
  isolationIndicator: "1",
  applyUser: userInfo.userId,
  applyDeptId: useUserStore.user.currentSelectDept,
  anesthesiaMethod: "",
  plannedSort: 1,
  emergencyFlag: "",
  surgeryBodyPart: "",
  surgeryStatus: "1",
  orgId: userInfo.orgId,
  createDate: "",
  updateDate: "",
  remarks: "",
  hivFlag: "N",
  hbvFlag: "N",
  syphilisFlag: "N",
  washHands: "Y",
  applyDeptName: useUserStore.user.currentSelectDeptName,
  applyUserName: userInfo.nickName
})

const rules = ref<any>({
  scheduledDate: [
    {
      required: true, message: "手术时间不能为空", trigger: "change"
    }
  ],
  surgeryId: [
    {
      required: true, message: "手术室不能为空", trigger: "change"
    }
  ],
  surgeryRoomId: [
    {
      required: true, message: "手术间不能为空", trigger: "change"
    }
  ],
  emergencyFlag: [
    {
      required: true, message: "手术类型不能为空", trigger: "change"
    }
  ],
  anesthesiaDeptId: [
    {
      required: true, message: "麻醉科室不能为空", trigger: "change"
    }
  ],
  anesthesiaMethod: [
    {
      required: true, message: "麻醉方法不能为空", trigger: "change"
    }
  ],
  plannedSort: [
    {
      required: true, message: "计划台次不能为空", trigger: "blur"
    }
  ],
  patientCondition: [
    {
      required: true, message: "病情说明不能为空", trigger: "change"
    }
  ],
  provideWay: [
    {
      required: true, message: "供血方式不能为空", trigger: "change"
    }
  ],
  isolationIndicator: [
    {
      required: true, message: "隔离标志不能为空", trigger: "change"
    }
  ],
  hivFlag: [
    {
      required: true, message: "艾滋标记不能为空", trigger: "change"
    }
  ],
  hbvFlag: [
    {
      required: true, message: "乙肝标记不能为空", trigger: "change"
    }
  ],
  syphilisFlag: [
    {
      required: true, message: "是否梅毒不能为空", trigger: "change"
    }
  ],
  washHands: [
    {
      required: true, message: "是否洗手不能为空", trigger: "change"
    }
  ]
})


// 表单重置
function reset() {
  form.value = {
    inoroutFlag: "1",
    surgeryNumber: 0,
    scheduledDate:
      "",
    surgeryApplyDate:
      "",
    surgeryId:
      "",
    surgeryName:
      "",
    surgeryRoomId:
      "",
    surgeryNoName:
      "",
    diagBeforeOperation:
      "",
    diagBeforeOperationCode:
      "",
    patientCondition:
      "",
    surgeryScale:
      "",
    isolationIndicator:
      "",
    applyUser:
      "",
    applyDeptId:
      "",
    anesthesiaMethod:
      "",
    emergencyFlag:
      "",
    surgeryBodyPart:
      "",
    provideWay:
      "1",
    surgeryStatus:
      "0",
    orgId: userInfo.orgId,
    plannedSort: 1,
  }

  nextTick(() => {
    surgeryNameListRef.value.emptyTableData()
    formRef.value.resetFields()
  })
}

/**
 * 手术室选择事件
 * @param obj
 */
function deptChange(obj: any) {
  form.value.surgeryId = obj.deptId
  form.value.surgeryName = obj.deptName
  listSurgeryRoom({"orgId": form.value.orgId, "surgeryId": obj.deptId}).then((res: resType) => {
    surgeryRoomList.value = res.data
    if (form.value.surgeryRoomId) {
      surgeryRoomNoChange(form.value.surgeryRoomId)
    }
  })
}

/**
 * 手术间选择事件
 * @param value
 */
function surgeryRoomNoChange(value: string) {
  form.value.surgeryRoomId = value
  form.value.surgeryNoName = surgeryRoomList.value.find(item => item.surgeryRoomId === value)?.roonNo
}

/**
 * 麻醉科室选择事件
 */
function anesthesiaDeptIdChange(obj: any) {
  form.value.anesthesiaDeptId = obj.deptId
  form.value.anesthesiaDeptName = obj.deptName
}

function validateForm() {
  // 先进行表单自身的校验
  return new Promise((resolve, _reject) => {
    (formRef.value as any).validate((valid: boolean) => {
      if (valid) {
        // 表单校验通过后，调用子组件的校验
        if (surgeryNameListRef.value && surgeryNameListRef.value.validateSurgeryList) {
          const surgeryListValid = surgeryNameListRef.value.validateSurgeryList();
          resolve(surgeryListValid);
        } else {
          resolve(true); // 如果子组件不存在或没有校验方法，则默认通过
        }
      } else {
        resolve(false);
      }
    });
  });
}

// 添加方法获取表单和手术列表数据
function getFormAndSurgeryData() {
  const surgeryApplyDetailList = surgeryNameListRef.value && surgeryNameListRef.value.getSurgeryList
    ? surgeryNameListRef.value.getSurgeryList()
    : [];
  return {
    formData: form.value,
    surgeryApplyDetailList
  };
}

function getApplyForm(obj: any) {
  form.value = obj
  deptChange({deptId: obj.surgeryId})
}

// 导出方法供父组件调用
defineExpose({
  validateForm,
  getFormAndSurgeryData,
  getApplyForm,
  reset
});


</script>


<style scoped>
.input-width {
  width: 100px;
}
</style>
